package logic

import (
	"context"
	"monitor/shared"
	"monitor/user/api/internal/svc"
	"monitor/user/api/internal/types"
	"time"

	"github.com/tal-tech/go-zero/core/logx"
)

type CmdSetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCmdSetLogic(ctx context.Context, svcCtx *svc.ServiceContext) CmdSetLogic {
	return CmdSetLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CmdSetLogic) CmdSet(req types.CmdSetReq) (*types.CmdSetResp, error) {
	// todo: add your logic here and delete this line
	err := l.svcCtx.Cache.SetWithExpire(shared.CacheSnPrefix+req.Sn, req.Cmd, 60*time.Duration(req.Expire)*time.Second)
	if err != nil && err == shared.ErrNotFound {
		return &types.CmdSetResp{Code: 1, Msg: "cmd set Cache error"}, nil
	}
	return &types.CmdSetResp{Code: 0, Msg: "ok"}, nil
}
